function fn_wrap(raw, success, fail){
    var resp = eval(raw);
    var state = resp[0];
    var data = resp[1];
    if (state == '200') {
        if (success)
            return success(state, data);
    } else {
        if (fail)
            return fail(state, data);
    }
}
//search
function search_do(qs, fn, fn_fail) {
    $.get('/search/'+ qs,
        function (data) {
            fn_wrap(data, fn, fn_fail); 
        });
}
//notes
function get_note_list_do(fn, fn_fail) {
    $.get('/notes/', 
        function (raw) {
            fn_wrap(raw, fn, fn_fail)
        });
}

function add_note_do(title, fn, fn_fail) {
    $.get('/note/add/' + title,
        function (raw) {
            fn_wrap(raw, fn, fn_fail);
        });
}

function remove_note_do(id, fn, fn_fail) {
    $.get('/note/' + id +'/remove',
        function(data){
            fn_wrap(data, fn, fn_fail);
        });
}

function rename_note_do(id, title, fn, fn_fail) {
    $.get('/note/' + id +'/rename/' + title,
        function(data){
            fn_wrap(data, fn, fn_fail);
        });
}


function get_note_do(id, fn, fn_fail) {
    $.get('/note/' + id,
        function (raw) {
            fn_wrap(raw, fn , fn_fail);
        });
}

//block 
function get_block_list_do(note_id, fn, fn_fail) {
    $.get('/note/' + note_id + '/blocks/', function (raw) {
        fn_wrap(raw, fn, fn_fail);
    });
}

function save_block_pos_do(order_ids, fn, fn_fail) {
    $.post('/blocks/save-pos', 
        {'ordered_ids': order_ids.toString()},
        function (raw) {
            fn_wrap(raw, fn , fn_fail);
        });
}

function save_block_content_do(id, content, fn, fn_fail) {
    $.post('/block/'+id+'/save-content', 
        {'content':content}, 
        function(raw) {
            fn_wrap(raw, fn, fn_fail);
        });
}

function save_block_tag_do(block_id, tags, fn, fn_fail) {
    $.post('/block/'+ block_id+'/save-tags', 
        {'tags': tags.toString() },
        function (raw) {
            fn_wrap(raw, fn, fn_fail);
        });
}


function add_block_do(note_id, content, fn, fn_fail) {
    $.get('/note/'+ note_id +'/add-block',
        {'content':content}, 
        function (raw) {
            fn_wrap(raw, fn, fn_fail);
        }
    );
}


function remove_block_do(id, fn, fn_fail) {
    $.get('/block/'+id+'/remove',
        function(data){
            fn_wrap(data, fn, fn_fail);
        });
}


function save_block_state_do(id, state, fn, fn_fail) {
    $.get('/block/'+id+'/save-state/'+state, 
        function (raw) {
            fn_wrap(raw, fn , fn_fail);
        });
}


function update_block_tags_do(block_id, fn, fn_fail) {
    $.get('/tags/' + block_id, function (data) {
        fn_wrap(data, fn, fn_fail); 
    });
}

function save_block_states_do(state_pairs, fn, fn_fail) {
    $.post('/blocks/save-state', 
        {'state_pairs': state_pairs.toString()},
        function (raw) {
            fn_wrap(raw, fn , fn_fail);
        });
}



